home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_3 / lusolv.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  612 b   |  22 lines  |  [MATF/MATL]

  1. function  [X,Y] = lusolv(A,B,row)
  2. % [X,Y] = lufact(A,B,row)
  3. % A is the nxn matrix obtained from lufact, input.
  4. % The matrix A must be in factored LU form,
  5. % and row is the row permutation information, input.
  6. % B is any nx1 vector, input.
  7. % The solution to AX=B  is X, output.
  8. % The solution to LY=PB is Y, output.
  9. % Forward substitution followed
  10. % by back substitution is used.
  11. [n n] = size(A);
  12. Y = zeros(n,1);
  13. Y(1) = B(row(1));
  14. for k = 2:n,
  15.   Y(k) = B(row(k)) - A(row(k),1:k-1)*Y(1:k-1);
  16. end
  17. X = zeros(n,1);
  18. X(n) = Y(n)/A(row(n),n);
  19. for k = n-1:-1:1,
  20.   X(k) = (Y(k) - A(row(k),k+1:n)*X(k+1:n))/A(row(k),k);
  21. end
  22.